package com.ticket.sass.admin.model

import com.google.gson.Gson
import com.ticket.sass.admin.util.GsonUtil
import com.ticket.sass.common.exception.enums.Code
import com.ticket.sass.common.exception.enums.ErrorCode
import io.swagger.annotations.ApiModel
import io.swagger.annotations.ApiModelProperty

@ApiModel("返回结果封装")
data class ResultModel<T>(
    @ApiModelProperty(value = "状态码：0 成功，1 失败", required = true)
    val code: Int = Code.OK.value,

    @ApiModelProperty(value = "信息", required = true)
    val message: String = "",

    @ApiModelProperty(value = "错误码", required = true)
    val errorCode: String = ErrorCode.OK.value,

    @ApiModelProperty(value = "错误信息", required = true)
    val errorMessage: String = "",

    @ApiModelProperty(value = "响应数据", required = true)
    val data: T? = null
) {
    companion object {
        @JvmStatic
        fun <T>success(data: T?): ResultModel<T> {
            return ResultModel(Code.OK.value, "SUCCESS", data)
        }

        @JvmStatic
        fun success(): ResultModel<String> {
            return success("")
        }
    }

    constructor(
            code: Int = Code.OK.value,
            message: String = "",
            data: T? = null
    ) : this(code, message, "", "", data)

    constructor(
            errorCode: String = "",
            errorMessage: String = "",
    ) : this(Code.ERROR.value, "", errorCode, errorMessage, null)



    override fun toString(): String {
        return GsonUtil.getInstance().toJson(this)
    }
}
